var express = require('express');
const { connection } = require('../model/db') // 获取连接实例
const jwt = require('jsonwebtoken');
const router = express.Router()


// 示例
// router.get("/msglist", (req, res) => {
//     connection.query(`select * from user;`, (err, data) => {
//         if (err) {
//             console.log("err", err)
//             res.send('query error')
//         } else {
//             // 将 MySQL 查询结果作为路由返回值
//             res.send({
//                 code: 200,
//                 msg: "获取数据成功",
//                 data
//             })
//         }
//     })
// })
router.post('/studentLogin', (req, res) => {
    //学生端传入的姓名和身份证号
    let { stu_name, card_id } = req.body
    let sql = `select s.id,stu_name,card_id,classroom,seat,college_id,dept_name from student s join department d on s.college_id=d.id where stu_name='${stu_name}' and card_id='${card_id}';`
    connection.query(sql, (err, data) => {
        if (err) {
            console.log("err", err)
            res.send('query error')
        } else {
            // 将 MySQL 查询结果作为路由返回值
            if (data.length !== 0) {
                if (data[0].card_id == card_id) {
                    let token = "Bearer " + jwt.sign({ stu_name, card_id }, "yv", { expiresIn: 10 * 600000 })
                    res.send({
                        code: 200,
                        msg: "获取数据成功",
                        data,
                        token
                    })
                } else {
                    res.send({
                        code: 304,
                        msg: "该学生信息不正确",
                    })
                }

            } else {
                // let sql2 = `insert into student(stu_name,card_id,classroom,college_id,seat,phone) values('${stu_name}','${card_id}','${req.body.classroom}','${req.body.college_id}','${req.body.seat}','${req.body.phone}')`
                // connection.query(`${sql2}`, (err, data) => {
                //     if (err) {
                //       console.log("err", err);
                //       res.send({
                //         code: 400,
                //         msg: "query error",
                //         err
                //       });
                //     } else {
                //       res.send({
                //         code: 200,
                //         msg: "添加学生成功"
                //       })
                //     }
                //   });

                res.send({
                    code: 304,
                    msg: "该学生信息不存在"
                })

            }
        }

    })
})

// 根据学生id查询当前学生的信息
router.get("/getstudentinfo",(req,res)=>{
    let id = req.query.id
    let sql = `select s.*,d.dept_name from student s join department d on s.college_id=d.id where s.id='${id}';`
    connection.query(`${sql}`, (err, data) => {
        if (err) {
          console.log("err", err);
          res.send({
            code: 400,
            msg: "query error",
            err
          });
        } else {
          res.send({
            code: 200,
            msg: "获取数据成功",
            obj: data[0]
          });
        }
      });
})

module.exports = router;